package com.yonyou.ucf.mdf.app.isvdxq.exception.controller;

import com.yonyou.ucf.mdf.app.isvdxq.ReturnResult.DataResult;
import com.yonyou.ucf.mdf.app.isvdxq.common.DxqConfig;
import com.yonyou.ucf.mdf.app.isvdxq.common.NoNeedToken;
import com.yonyou.ucf.mdf.app.isvdxq.exception.entity.ExceptionEntity;
import com.yonyou.ucf.mdf.app.isvdxq.exception.service.ExceptionEntityService;
import com.yonyou.ucf.mdf.app.isvdxq.other.service.BaseService;
import com.yonyou.ucf.mdf.app.isvdxq.utils.RequestTool;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = {DxqConfig.pre_apiUrl+"/exception",DxqConfig.pre_pcUrl+"/exception"})
public class ExceptionEntityController {
    @Autowired
    private BaseService baseService;
    @Autowired
    private ExceptionEntityService exceptionEntityService;

    //插入异常信息
    @PostMapping("/InsertException")
    public DataResult InsertException(@RequestBody ExceptionEntity exception, String tenant_id) {
        return exceptionEntityService.insertException(exception, tenant_id);
    }

    @RequestMapping("/ViewInfo")
    public DataResult ViewInfo(HttpServletRequest request, String id) {
        DataResult result = new DataResult(0, "操作失败");
        //接收参数
        String tenantId = baseService.getTenantId(request);
        try {
            Map params = RequestTool.getParamsPage(request, result);
            if (StringUtils.isEmpty(id)) return result;
            ExceptionEntity exceptionEntity = exceptionEntityService.SelectById(id);
            result = new DataResult(1, "操作成功");
            result.setData(exceptionEntity);
        } catch (Exception ex) {
            result.setMessage(ex.getMessage());
        }
        return result;
    }

    @RequestMapping("/ViewPage")
    public DataResult ViewPage(HttpServletRequest request) {
        DataResult result = new DataResult(0, "操作失败");
        //接收参数
        String tenantId = baseService.getTenantId(request);
        try {
            Map params = RequestTool.getParamsPage(request, result);
            int pagenum = result.getPageNum();
            int pagesize = result.getPageSize();
            int m = result.initStartNumber();
            List<ExceptionEntity> dataList = exceptionEntityService.SelectPage(tenantId, m, pagesize);
            result = new DataResult(1, "操作成功");
            result.setDataListEx(dataList);
        } catch (Exception ex) {
            result.setMessage(ex.getMessage());
        }
        return result;
    }
    /**
     * 所有数据列表
     * @param request
     * @return
     */
    @NoNeedToken
    @RequestMapping("/ViewList")
    public DataResult ViewList(HttpServletRequest request) {
        DataResult result = new DataResult(0, "操作失败");
        //接收参数
        String tenantId = baseService.getTenantId(request);
        List<ExceptionEntity> dataList = exceptionEntityService.SelectAll(tenantId);
        result = new DataResult(1, "操作成功");
        result.setDataList(dataList);

        return result;
    }
}
